package com.shadow.baiqun.delegate.receive.goodscheck;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.shadow.baiqun.config.ItemField;
import com.shadow.baiqun.config.ItemType;
import com.shadow.core.util.date.DateUtils;
import com.shadow.ui.recycler.DataConverter;
import com.shadow.ui.recycler.MultipleItemEntity;
import com.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;

/**
 * @author zy
 * on 2018/6/26
 */
public class GoodsCheckConverter extends DataConverter {
    private final boolean isEdit;

    public GoodsCheckConverter(boolean isEdit) {
        this.isEdit = isEdit;
    }

    @Override
    public ArrayList<MultipleItemEntity> convert() {
        try {
            final JSONArray jsonArray = JSONObject.parseObject(getJsonData())
                    .getJSONArray("rows");
            final int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
                final JSONObject object = jsonArray.getJSONObject(i);
                GoodsCheckEntry goodsCheckEntry = new ObjConverter<GoodsCheckEntry>().
                        convert(object.toJSONString(), new TypeReference<GoodsCheckEntry>() {
                        });
                String sendTime = goodsCheckEntry.getPrepgoodsCSendtime();
                if (sendTime != null) {
                    goodsCheckEntry.setPrepgoodsCSendtime(DateUtils.longToString(
                            Long.valueOf(sendTime)));
                }
                MultipleItemEntity itemEntity = MultipleItemEntity.builder()
                        .setItemType(isEdit ? ItemType.CAN_EDIT : ItemType.CAN_NOT_EDIT)
                        .setField(ItemField.ITEM_OBJ, goodsCheckEntry)
                        .build();

                mEntities.add(itemEntity);
            }
            return mEntities;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
